package com.woniuxy.microfixeddepositserver.dao;


import com.woniuxy.bean.regularbean.MemberAccountInnerBean;
import com.woniuxy.microfixeddepositserver.bean.FixedTimeDepositBean;
import com.woniuxy.microfixeddepositserver.bean.detail.RegularQuery;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface FixedTimeDepositDao {
    /**
     * 根据id下架或者删除该理财产品
     */
    int deleteByPrimaryKey (@Param("ids") List<String> ids);

    /**
     * 根据id上架该理财产品
     */
    int recoverRegular (@Param("ids") List<String> ids);

    /**
     * 新增理财产品
     */
    int insert (FixedTimeDepositBean fixedTimeDepositBean);

    /**
     * 根据id查询理财产品
     */
    FixedTimeDepositBean selectByPrimaryKey (@Param("id") Integer id, @Param("status") Integer status);

    /**
     * 分页查询所有理财产品信息
     */
    List<FixedTimeDepositBean> selectAll (@Param("status") Integer status, @Param("isHot") Integer isHot);

    /**
     * 修改理财产品信息
     */
    int updateByPrimaryKey (FixedTimeDepositBean fixedTimeDepositBean);

    /**
     * 根据条件分页查询所有理财产品信息
     */
    List<FixedTimeDepositBean> selectAllByQuery (@Param("regularQuery") RegularQuery regularQuery);

//    /**
//     * 根据条件查询产品风险等级,总份额,最低购买金额
//     */
//    @Select("select risk_grade riskGrade,sum_count sumCount,minimum_money minimumMoney from " +
//            "fixed_time_deposit where id=#{fid} and status=1")
//    FixedTimeDepositBean getRiskGradeId (@Param("fid") Integer fid);

    /**
     * 扣除该定期产品的总份额及增加销售数量
     */
    @Update("update fixed_time_deposit set sum_count=sum_count-1,selled_count=selled_count+1," +
            "sum_sale=sum_sale + #{money} where id=#{fid} and status=1")
    void subTotalAndAddSelCount (@Param("fid") Integer fid, @Param("money") Double money);

    //获取指定用户所有购买的定期产品收益信息
    @Select("select member_account memberAccount,max(fixed_deposit_id) fixedDepositId,sum(confirmCapital) confirmCapital," +
            "sum(unConfirmCapital) unConfirmCapital,sum(over_all_balance) overAllBalance,sum(today_earnings) todayEarnings," +
            "sum(total_earnings) totalEarnings from " +
            "member_fixed_account where member_account=#{userName} and status=1 group by fixed_deposit_id")
    List<MemberAccountInnerBean> getRegularEarnings (@Param("userName") String userName);

    //根据订单号获取指定用户购买的定期产品收益信息
    @Select("select id aid,member_account memberAccount,fixed_deposit_id fixedDepositId,confirmCapital,unConfirmCapital," +
            "over_all_balance overAllBalance,today_earnings todayEarnings,total_earnings totalEarnings, dute_date duteDate,buyDay from " +
            "member_fixed_account where id=#{id} and status=1")
    MemberAccountInnerBean getEarningsById ( @Param("id") Integer id);

//    //查询用户购买该产品的时间
//    @Select("select parchase_day from member_fixed_account where member_account=#{userName} and id=#{id}  and status=1")
//    MemberAccountInnerBean getPachaseDay (@Param("userName") String userName, @Param("id") Integer id);

    //获取所有用户购买的定期产品信息
    @Select("select id aid,member_account memberAccount,fixed_deposit_id fixedDepositId,confirmCapital,unConfirmCapital," +
            "over_all_balance overAllBalance,calculate_earnings_day calculateEarningsDay,today_earnings todayEarnings,total_earnings " +
            "totalEarnings ,yield_rates yieldRates from " +
            "member_fixed_account where  status=1")
    List<MemberAccountInnerBean> getRegulars ();

    //将用户个人定期数据插入数据库
    void insertAsset (MemberAccountInnerBean innerBean);

    //更新收益信息
    void updateEarnings (List<MemberAccountInnerBean> list);

    //根据资产id号查询产品id
    @Select("select fixed_deposit_id from member_fixed_account where id=#{id} and status = 1")
    Integer getProductIdById (@Param("id") Integer id);

    //删除跟人资产中心对应数据
    @Update("update member_fixed_account set status=status+1 where id=#{id}")
    int removeMemberEarningsInfo (@Param("id") Integer id);

    //后台管理系统查询商品列表
    List<FixedTimeDepositBean> selectAllNotHot (Integer status);

    @Update("update fixed_time_deposit set isHot=#{isHot} where id=#{fid}")
    int updateIsHot (@Param("fid") Integer fid, @Param("isHot") Integer isHot);

//    List<FixedTimeDepositBean> selectAllProduct (Integer status);

    @Select("select deduct from fixed_time_deposit where id=#{id} and status=1")
    Integer getPlatformDeduct (@Param("id") Integer aid);
}